package com.yksj.healthtalk.ui.setting;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.open.share.OpenManager;
import com.open.share.interfaces.IOpenResponse;
import com.open.share.net.OpenResponse;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.comm.LodingFragmentDialog;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.utils.ShareUtils;
import com.yksj.healthtalk.utils.ToastUtil;
/**
 * 微博分享
 * @author root
 *
 */
public class SettingShareActivity extends BaseFragmentActivity implements OnClickListener {
	TextView textNum;
	private EditText content;
	private LodingFragmentDialog showLodingDialog;
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.setting_share_layout);
		initView();
	}

	private void initView() {
		initTitle();
		titleLeftBtn.setOnClickListener(this);
		titleRightBtn2.setVisibility(View.VISIBLE);
		titleRightBtn2.setText("确定");
		titleRightBtn2.setOnClickListener(this);
		titleTextV.setText("邀请微博好友");
		titleTextV.setTextColor(Color.BLACK);
		textNum=(TextView)findViewById(R.id.text_num);
		content = (EditText)findViewById(R.id.content);
		content.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				textNum.setText(s.toString().length()+"");
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}
			@Override
			public void afterTextChanged(Editable s) {
			}
		});
		
	}
	/**
	 * 初始化数据
	 */
	private void sendMessage() {
		showLodingDialog = LodingFragmentDialog.showLodingDialog(getSupportFragmentManager(), "马上来了");
		String contentText = content.getText().toString().trim();
		Bundle bundle = new Bundle();
		bundle.putString(OpenManager.BUNDLE_KEY_TEXT,contentText);
		ShareUtils.share(OpenManager.SINA_WEIBO, bundle,this,new IOpenResponse() {
			@Override
			public boolean response(int arg0,final Object arg1) {
				if(!isFinishing()){
					runOnUiThread(new Runnable() {
						public void run() {
							showLodingDialog.dismissAllowingStateLoss();
							OpenResponse response=(OpenResponse) arg1;
							if(response.ret==0){
								ToastUtil.showBasicShortToast(getApplicationContext(), "发送成功");finish();
							}else if(response.ret == 20019){
								ToastUtil.showBasicShortToast(getApplicationContext(), "你已分享过");
							}else{
								ToastUtil.showBasicShortToast(getApplicationContext(), "分享失败");
						}
						}
					});
				}
				return true;
			}
		});
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_back:
			finish();
			overridePendingTransition(R.anim.back_enter, R.anim.back_exit);
			break;
		case R.id.title_right2:
			//字数小于140
			if(0!=content.getText().toString().trim().length()&&140>content.getText().toString().trim().length()){
				sendMessage();
			}else{
				ToastUtil.showBasicShortToast(SettingShareActivity.this, "发表字数超出");
				return;
			}
			break;
		}
		
	}
}
